<?php
/**
 * Created by PhpStorm.
 * User: peter
 * Date: 2018/10/19
 * Time: 上午10:18
 */

namespace app\activity\logic;


use app\model\ActivityCollectionPoint;
use app\model\ActivityOrderAttach;
use extend\Functions;
use extend\STATUS_CODE;

class CollectionPoint
{
    /**
     * @purpose 根据团期ID获取集合点
     * @param int $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function itemByGroupStageId (int $id): array
    {
        $model      = new ActivityCollectionPoint();
        $result     = $model
            ->alias('acp')
            ->where('acp.activity_group_stage_id', $id)
            ->field('acp.id,acp.address,acp.status,acp.title,
            (select count(1) from '.ActivityOrderAttach::M()->table.' where (`status` = 4 or `status` = 99) and collection_point_id=`acp`.`id`) as sign_uped_limit,
            FROM_UNIXTIME(acp.set_out_time, "%H:%i") as set_out_date,acp.sign_up_limit')
            ->order('acp.status asc')
            ->select();
        $items = $result->toArray();
        foreach ($items as &$item) {
            $item['set_out_date'] = '';
            $item['is_than_limit'] = ($item['sign_uped_limit'] < $item['sign_up_limit']) ? 1 : 2;
        }
        $items = Functions::bubble_sort($items, 'is_than_limit');
        return Functions::result(
            STATUS_CODE::SUCCESS,
            $items
        );
    }
}